/**
 * Created by mac on 15-12-6.
 */
tools = {
    webmaster_path  :   'webmaster',
    web_site        :   'http://master.odingweb.com/',
    alert:function(msg,fn,title){
        title = title?title:'信息提示';
        var html='';
        html += '<div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="alert_nodal" class="modal fade">';
        html +=     '<div class="modal-dialog">';
        html +=         '<div class="modal-content">';
        html +=             '<div class="modal-header">';
        html +=                     '<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>';
        html +=                     '<h4 class="modal-title">'+title+'</h4>';
        html +=             '</div>';
        html +=         '<div class="modal-body">'+msg+'</div>';
        html +=         '<div class="modal-footer"><button type="button" id="yes_btn" data-dismiss="modal" class="btn btn-success">确 定</button></div>';
        html +=         '</div>';
        html +=     '</div>';
        html += '</div>';
        msg='';
        if(!$('#alert_nodal').length){
            $('body').append(html);
        } else {
            $('#alert_nodal .modal-title').html(title);
            $('#alert_nodal .modal-body').html(msg);
        }
        $('#alert_nodal').modal('show');
        $('#alert_nodal').on('hidden.bs.modal', function (e) {
            fn && fn();
            $('#alert_nodal').remove();
        });
    },
    confirm:function(msg,fn,title){
        title = title?title:'信息提示';
        var html='';
        html += '<div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="alert_nodal" class="modal fade">';
        html +=     '<div class="modal-dialog">';
        html +=         '<div class="modal-content">';
        html +=             '<div class="modal-header">';
        html +=                     '<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>';
        html +=                     '<h4 class="modal-title">'+title+'</h4>';
        html +=             '</div>';
        html +=             '<div class="modal-body">'+msg+'</div>';
        html +=             '<div class="modal-footer">';
        html +=                 '<button type="button" data-dismiss="modal" class="btn btn-default">取 消</button>';
        html +=                 '<button type="button" id="yes_btn" class="btn btn-success">确 定</button>';
        html +=             '</div>';
        html +=         '</div>';
        html +=     '</div>';
        html += '</div>';
        msg='';
        if(!$('#alert_nodal').length){
            $('body').append(html);
        } else {
            $('#alert_nodal .modal-title').html(title);
            $('#alert_nodal .modal-body').html(msg);
        }
        $('#alert_nodal').modal('show');
        $('#alert_nodal').on('hidden.bs.modal', function (e) {
            $('#alert_nodal').remove();
        });
        $('#yes_btn').click(function(){
    		fn && fn();
    		$('#alert_nodal').modal('hide');
        });
    },
    validate:function(form,success_fn,fail_fn){
        var url=$('input[name="action"]').val();
        !url && alert('请填写action隐藏域');
        form.submit(function(){return false;});
        form.validate({
            onfocusout  : false,
            onkeyup     : false,
            'submitHandler': function () {
                var oBtn=$('input:submit,button:submit');
                oBtn.attr('disabled','disabled');
                if($('#editor').length){
        			var contents="&contents="+UE.getEditor('editor').getContent();
        		}
                $.post(url, form.serialize()+contents, function (data) {
                    if (data.ret == 1) {
                        success_fn && success_fn(data);
                    } else {
                        fail_fn && fail_fn(data);
                        oBtn && oBtn.attr('disabled', false);
                    }
                }, 'json');
            },
            showErrors: function(errorMap, errorList) {
                var msg = "";
                $(errorList).each(function(k,v){
                    msg+= v.message+'<br>';
                });
                if(msg!=""){
                    tools.alert(msg);
                }
            }
        });
    },
    list_del:function(){
        $('.del-btn').click(function($where){
            var url    =   $(this).attr('link');
            var id     =   $(this).attr('data-id');
            if(!url || !id){
                alert('请填写属性link及id');
                return;
            }
            tools.confirm('你确定删除该项目？',function(){
                $.post(url,{
                    'id'    : id
                },function(data){
                    if(data.ret==1){
                        window.location.reload();
                    } else {
                        $('#alert_nodal').modal('hide');
                        $('#alert_nodal').on('hidden.bs.modal', function (e) {
                            tools.alert(data.msg);
                        });
                    }
                },'json');
            });
        });
    },
    img_del:function(){
        $('.img_del').click(function($where){
            var url    =   $(this).attr('link');
            var id     =   $(this).attr('data-id');
            if(!url || !id){
                alert('请填写属性link及id');
                return;
            }
            tools.confirm('你确定删除该项目？',function(){
                $.post(url,{
                    'id'    : id
                },function(data){
                    if(data.ret==1){
                        $('#alert_nodal').modal('hide');
                        $('#img_detail img').remove();
                        $('input[name="hid_img_0"]').val('');
                    } else {
                        $('#alert_nodal').modal('hide');
                        $('#alert_nodal').on('hidden.bs.modal', function (e) {
                            tools.alert(data.msg);
                        });
                    }
                },'json');
            });
        });
    },
    select_all:function(){ //反选
    	$('.select_all').click(function(){
    		$('input[name="group_del[]"]').each(function(k,v){
    			if($(v).attr('checked')=='checked'){
    				$(v).attr('checked',false);
    			} else {
    				$(v).attr('checked','checked');
    			}
    		});
    	});
    },
    list_order:function($db_name,action){
		$('.list-order').click(function(){
			var list_id	='';
			var list_val	='';
			$('input[name="my_order[]"]').each(function(k,v){
				list_id		+= $(v).attr('list_id')+',';
				list_val		+= $(v).val()+',';
			});
			list_id	= list_id.substr(0,list_id.length-1);
			list_val	= list_val.substr(0,list_val.length-1);
			$.post('/'+tools.webmaster_path+action,{
				'list_id' : list_id,
				'list_val': list_val,
				'db_name' : $db_name
			},function(data){
				if(data.ret==1){
					window.location.reload();
				}
			},'json');
		});
    },
    group_del:function(action){ //批量删除
    	var flag=false;
    	$('.group_del').click(function(){
    		var group_id='';
    		$('input[name="group_del[]"]').each(function(k,v){
    			if($(v).attr('checked')=='checked'){
    				var val=$(v).val();
    				group_id += val+'|';
    				flag=true;
    			}
    		});
    		if(!flag){
    			tools.alert('请选择需要删除的项目');
    			return;
    		}
    		tools.confirm('您确定删除该项目？',function(){
    			$.post('/'+tools.webmaster_path+action,{
    				'group_id'	: group_id
    			},function(data){
    				if(data.ret==1){
    					window.location.reload();
    				}
    			},'json');
    		});
    	});
    },
    create_editor:function(){
    	var len = $('.editor').size();
    	for(var i=0;i<len;i++){
	    	var ue = UE.getEditor('editor_'+i,{
				scaleEnabled:true,
				initialFrameWidth:'100%',
				initialFrameHeight:480
			});
		}
    },
    mulupload:function(){
    	var upload_num	= $('input[name="upload_num"]').val();
    	if(!upload_num){
    		alert('请传入和upload_num');
    		return;
    	}
    	$('.del-img-btn').on('click',function(){
    		var btn = this;	    		
    		tools.confirm('你确定删除该图片吗？',function(){
    			var id		= $(btn).attr('data-id');
	    		var num 	= $(btn).attr('data-num');
	    		var action	= $(btn).attr('data-action');
	    		(!id || !num || !action) && alert('请传入id,num,action');
	    		if(id){
	    			$.post('/'+tools.webmaster_path+action,{
	    				'id'		: id,
	    				'num'	: num
	    			},function(data){
	    				if(data.ret==1){
	    					var parent=$(btn).parent().parent();
	    					$(parent).remove();
	    				}
	    			},'json');
	    		} 
    		});
    	});
    	var img_id_upload=new Array();//初始化数组，存储已经上传的图片名
		var i=0;//初始化数组下标
		$(function() {
		    $('#file_upload').uploadify({
		    	'auto'     		: false,//关闭自动上传
		    	'removeTimeout' : 10000000000,//文件队列上传完成1秒后删除
		        'swf'        : 'js/upload/uploadify.swf',
		        'uploader'   : '/static/uploadify.php',
		        'method'   	 : 'post',//方法，服务端可以用$_POST数组获取数据
				'buttonText' : '选择图片',//设置按钮文本
		        'multi'    : true,//允许同时上传多张图片
		        'uploadLimit' : 9999,//一次最多只允许上传9999张图片
		        'fileTypeDesc' : 'Image Files',//只允许上传图像
		        'fileTypeExts' : '*.gif; *.jpg; *.png',//限制允许上传的图片后缀
		        'fileSizeLimit' : '50000KB',//限制上传的图片不得超过500KB 
		        'onUploadSuccess' : function(file, data, response) {//每次成功上传后执行的回调函数，从服务端返回数据到前端
			        	//data前面出现一些乱码需要重新拼接
			        	var url=data.toString();
			        	var ary=url.split("/");
			        	url= tools.web_site+ary[1]+'/'+ary[2]+'/'+ary[3];
			        	var path = '/'+ary[1]+'/'+ary[2]+'/'+ary[3];
			        	$('#'+file.id).find('.mul_img').find('.info').html('<img src="'+url+'" width="150"><input type="hidden" name="mul_upload[]" value="'+path+'">');
			        	$('#'+file.id).find('.mul_img').find('.mid').html('');
			        	$('#'+file.id).find('.del-img-btn').click(function(){
			        		$('#file_upload').uploadify('cancel',file.id);
			        	});
		        },
		        'onQueueComplete' : function(queueData) {//上传队列全部完成后执行的回调函数
		           
		        },
		        'onUploadError': function(a){
		        	
		        },
		        file_queued_handler : function(file){
		        		var frame_has_num = $('.mul_upload_1 .mul_img_section').size();
		        		var html = '';
		        		$(file).each(function(k,v){
		        			i++;
		        			html+= '<div id="'+v.id+'" class="mul_img_section">';
		        			html	+= 		'<div class="mul_img">';
		        			html+=			'<span class="info">0%</span>';
		        			html+=			'<span class="mid">0%</span>';
		        			html+=			'<span class="btn btn-xs btn-danger del-img-btn"><i class="fa fa-minus-circle"></i></span>';
		        			html+=		'</div>';
		        			html+=	'</div>';                               			                  
		        		});
		        		if(frame_has_num>=upload_num){
		        			alert('允许上传'+upload_num+'张图片');
		        			return;
		        		}
		        		$('.mul_upload_1').append(html);
		        		$('#file_upload').uploadify('upload','*');
		        },
		        onUploadProgress:function(file, fileBytesLoaded, fileTotalBytes){
		        		var percentage       = Math.round(fileBytesLoaded / fileTotalBytes * 100);
		        		$('#'+file.id).find('.mul_img').find('.info').html(percentage+'%');
		        }
		    });
		});
    }
}